﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;

namespace KOPLibrary.AutoComplete
{
    [Serializable]
    public static class AutoComplateManager
    {
        private static TemplateInfoList list = null;
        public static string XmlPath
        {
            get { return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\KOP\AutoComplateData.xml"; }
        }
        public static TemplateInfoList List
        {
            get { return list; }
            set { list = value; }
        }
        
        public static void Startup()
        {        
            list = new TemplateInfoList();
            LoadList();
            Util.CheckUpdateWeekly();
        }

  

        private static void LoadList()
        {
            if (!File.Exists(XmlPath)) return;
            using (StreamReader sreader = new StreamReader(XmlPath))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(TemplateInfoList));
                StringReader reader = new StringReader(sreader.ReadToEnd());
                List = (TemplateInfoList)serializer.Deserialize(reader);                
                sreader.Close();
            }
        }

        public static AutoComplateInfoList GetData(string templatename)
        {
            AutoComplateInfoList ilist = List.FirstOrDefault(p => p.TemplateName == templatename) ?? new AutoComplateInfoList();
            ilist.TemplateName = templatename;
            return ilist;
        }
        public static void AddValue(AutoComplateInfoList infolist)
        {
            AutoComplateInfoList oldinfolist = GetData(infolist.TemplateName);
            int oldinfovalueindex, oldinfolistindex;
            string selecteditem;
            foreach (AutoCompleteInfo info in infolist.List)
            {
                selecteditem = info.Values[0].Trim();
                oldinfolistindex = oldinfolist.List.IndexOf(oldinfolist.List.SingleOrDefault(p => p.FieldName == info.FieldName));
                AutoCompleteInfo oldinfo;
                if (oldinfolistindex == -1)
                {
                    oldinfo = new AutoCompleteInfo();
                    oldinfo.FieldName = info.FieldName;
                    oldinfo.Values.Insert(0, selecteditem);
                    oldinfolist.List.Add(oldinfo);

                }
                else
                {
                    oldinfo = oldinfolist.List[oldinfolistindex];
                    oldinfovalueindex = oldinfo.Values.IndexOf(selecteditem);
                    if (oldinfovalueindex != -1) { oldinfo.Values.RemoveAt(oldinfovalueindex); }
                    oldinfo.Values.Insert(0, selecteditem);
                    oldinfolist.List[oldinfolistindex] = oldinfo;
                }
                oldinfolist.TemplateName = infolist.TemplateName;
                AutoComplateInfoList itemlist = list.FirstOrDefault(p => p.TemplateName == oldinfolist.TemplateName);
                int index = list.IndexOf(itemlist);
                if (index == -1)
                {
                    list.Add(oldinfolist);
                }
                else
                {
                    list[index] = oldinfolist;
                }
            }
        }

        public static void ShutDown()
        {            
            if (!Directory.Exists(Path.GetDirectoryName(AutoComplateManager.XmlPath)))
                Directory.CreateDirectory(Path.GetDirectoryName(AutoComplateManager.XmlPath));
            XmlSerializer serializer = new XmlSerializer(typeof(TemplateInfoList));
            StringWriter writer = new StringWriter();            
            serializer.Serialize(writer, AutoComplateManager.List);
            using (StreamWriter swriter = new StreamWriter(AutoComplateManager.XmlPath, false))
            {
                swriter.Write(writer.ToString());
                swriter.Close();
            }
            MailManagement.MailManager.RemoveAllTempFiles();
        }
    }
}